package com.five.dao.impl.backstage;

import com.five.dao.backstage.RuKuDao;
import com.five.entity.backstage.TRukudetai;
import com.five.util.DBUtil;
import org.springframework.stereotype.Repository;


import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: jianglin
 * Date: 2020/11/27  17:28
 */
@Repository
public class RuKuDaoImpl implements RuKuDao {


    @Override
    public int addRuKu(Map map) {
       String sql= "insert into t_ruku (rdate,uid,jsr,totalcount,totalprice) values (now(),?,?,?,?)";
        return DBUtil.getKey(sql,map.get("uid"),map.get("jsr"),map.get("totalcount"),map.get("totalprice"));
    }

    @Override
    public int addRuK(Map map1, int i) {
        String sql="insert into t_rukudetail(ssid,count,rukuid,price) values(?,?,?,?) ";
        return DBUtil.manage(sql,map1.get("facid"),map1.get("count"),i,map1.get("pmoney"));
    }

    @Override
    public int addRuKus(Map map1) {
        String sql="update t_facility set count=count+? where id=? ";
        return DBUtil.manage(sql,map1.get("count"),map1.get("facid"));
    }

    @Override
    public List<Map> query(Integer page, Integer limit, TRukudetai tRukudetai) {
        String sql="select x.ruid,x.totalprice,x.totalcount,y.id,y.name as name,x.jsr,x.rdate from t_ruku x,t_staff y where y.id=x.uid";


        if (tRukudetai.getName()!=null){
            sql+=" and name like '%"+tRukudetai.getName()+"%' ";
        }
        if (tRukudetai.getJsr()!=null){
            sql+=" and jsr like '%"+tRukudetai.getJsr()+"%' ";
        }

        if (tRukudetai.getKdata()!="" && tRukudetai.getKdata()!=null && tRukudetai.getEdata()!="" && tRukudetai.getEdata()!=null){
            sql+=" and rdate>'"+tRukudetai.getKdata()+"' and rdate<'"+tRukudetai.getEdata()+"' ";
        }
        sql+=" limit ?,? ";

        return DBUtil.queryMap(sql,(page-1)*limit,limit);
    }

    @Override
    public int count(TRukudetai tRukudetai) {
        String sql="select count(*) a from t_ruku x,t_staff y where y.id=x.uid";
        if (tRukudetai.getName()!=null){
           sql+=" and name like '%"+tRukudetai.getName()+"%' ";
        }
        if (tRukudetai.getJsr()!=null){
            sql+=" and jsr like '%"+tRukudetai.getJsr()+"%' ";
        }

        if (tRukudetai.getKdata()!="" && tRukudetai.getKdata()!=null && tRukudetai.getEdata()!="" && tRukudetai.getEdata()!=null){
            sql+=" and rdate>'"+tRukudetai.getKdata()+"' and rdate<'"+tRukudetai.getEdata()+"' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        String a = maps.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }

    @Override
    public List<Map> querySs(Integer id) {
        String sql="select * from t_facility where status=0 and typeid =?";
        return DBUtil.queryMap(sql,id);
    }

    @Override
    public List<Map> querySsType() {
        String sql="select * from t_ptype where status=0";
        return DBUtil.queryMap(sql);
    }
    //通过设施id查询设施
    @Override
    public List<Map> querySsTypeid(Integer id) {
        String sql="select * from t_facility where id=?";
        return DBUtil.queryMap(sql,id);
    }

    @Override
    public List<Map> queryRuId(Integer ruid) {
       String sql="select se.id id,rd.id id,se.name facname,tp.name typename,ru.ruid rukuid,se.gys gys,rd.count count,rd.price price,se.id sid from t_rukudetail rd,t_ruku ru,t_facility se,t_ptype tp WHERE rd.rukuid=ru.ruid AND rd.ssid=se.id AND se.typeid=tp.id AND ru.ruid=?";
        return DBUtil.queryMap(sql,ruid);
    }
}
